import _ from 'lodash';
import locales, { transformI18n } from '@src/locales/index.ts'
import { defaultCNKey } from '@src/util/languages'
import { useFormMultiLanguage } from '@hooks/useFormMultiLanguage'

const { isOpenMultiLanguage, languagesList } = useFormMultiLanguage()

export { isOpenMultiLanguage, languagesList, defaultCNKey }

/**
 * @description: 生成默认的多语言key-value对象
 * @return xxxx.language { 'zh-CN': '', 'en-US': '', 'ja': '', 'ko': ''}
 */
 export function getCommonDefaultLangForKey(key) {
	let obj = {};
	languagesList.forEach(item => {
		obj[item.language] = key && transformI18n(key, item.language) || '';
	});
	return obj;
}

/**
 * @description: 生成选项or多级联动的多语言树根节点
 * @return xxxLanguage { 'zh-CN': [], 'en-US': [], 'ja': [], 'ko': []}
 */
export function getDefaultLangOpts() {
	let obj = {};
	languagesList.forEach(item => {
		obj[item.languageKey] = [];
	});
	return obj;
}

/**
	 * @description: 格式化option 将插入的optionLanguage 剔除
	 * @param {Object} options 选项对象
	 * @return options
	 */
 export function formatOptions(options, typeKey = 'option') {
	const langKey = `language`;
	if (!options) return [];
	const OPTS = _.cloneDeep(options);
	return OPTS.map(opt => {
		opt.id = opt[typeKey];
		if (opt[langKey]) delete opt[langKey];
		return opt;
	});
}

/**
	 * @description: 初始化options 往options每项中插入对应的language 供给多语言组件数据
	 * @param {Object} optsLang optionsLanguage
	 * @return options
	 */
 export function initOptionLanguage(optsLang, options) {
	const langKey = `language`;
	let OPTS = _.cloneDeep(options);
	// TODO 待优化
	if (optsLang && Object.keys(optsLang).length) {
		OPTS.forEach((opt, index) => {
			// 防止在opt[langKey] = xxx 处循环引用
			const OPT = _.cloneDeep(opt);
			let idMap = {};
			Object.keys(optsLang).forEach(key => {
				idMap[key] = optsLang[key][index]
			});
			opt[langKey] = { [defaultCNKey]: OPT, ...idMap }
		});
		
	} else {
		OPTS.forEach(opt => {
			opt[langKey] = { ...getCommonDefaultLangForKey(), [defaultCNKey]: opt };
		});
	}
	return OPTS;
}

/**
	 * @description: 格式化option 生成标准的optionsLanguage
	 * @param {Object} options 选项对象
	 * @return optionsLanguage
	 */
 export function formatLanguage(options) {
	const langKey = `language`;
	if (!options) return [];
	let langOpts = getDefaultLangOpts();
	let OPTS = _.cloneDeep(options);
	OPTS.forEach(opt => {
		if (!opt[langKey]) return;
		Object.keys(opt[langKey]).forEach(key => {
			if (!langOpts[key]) return;
			// 默认需要为空字符，不然axios会把undefined处理成null（也可能是浏览器处理的，不是很确定），null的话，服务端那边处理会有问题~
			langOpts[key].push(opt[langKey][key] || '');
		});
	});
	return langOpts;
}

/**
 * @description: 支持多语言的模块
 * @param {String} mode 业务模块
 * @return Boolean
 */
export function isMultiLangModules(mode) {
	return true
  const modules = ['event', 'eventReceipt', 'product'];
  return modules.includes(mode);
}


/**
 * @des 处理没有国际化的旧表单数据
 */
export function setOldDefaultLang(field) {
  let key = `common.form.type.${field.formType}`;
  if (!field.displayNameLanguage) {
    field['displayNameLanguage'] = {
      ...getCommonDefaultLangForKey(key),
      [locales.locale]: field.displayName
    };
  } else {
    // 如果存在language 需要与当前languages做比较，补充缺省语言
    try {
      let la_ = {
        ...getCommonDefaultLangForKey(key),
        [locales.locale]: field.displayName
      };
      let baseLa = _.cloneDeep(field.displayNameLanguage);
      field.displayNameLanguage = {
        ...la_,
        ...baseLa
      }
    } catch (error) {
      console.warn(error, 'error try catch setOldDefaultLang displayName');
    }
  }
  if (!field.placeHolderLanguage) {
    field['placeHolderLanguage'] = {
      ...getCommonDefaultLangForKey(),
      [locales.locale]: field.placeHolder || ''
    };
  } else {
    // 如果存在language 需要与当前languages做比较，补充缺省语言
    try {
      let la_ = {
        ...getCommonDefaultLangForKey(),
        [locales.locale]: field.placeHolder || ''
      };
      let baseLa = _.cloneDeep(field.placeHolderLanguage);
      field.placeHolderLanguage = {
        ...la_,
        ...baseLa
      }
    } catch (error) {
      console.warn(error, 'error try catch setOldDefaultLang placeHolder');
    }
  }
  return field;
}